import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
import Home from '../views/Home.vue'
import Login from '../views/login/index.vue'
import Categroes from '../views/Categroes/create.vue'
import CategroesList from '../views/Categroes/List.vue'
import Items from '../views/Items/create.vue'
import ItemsList from '../views/Items/List.vue'
import Heroes from '../views/Heroes/create.vue'
import HeroesList from '../views/Heroes/List.vue'
import AdminUser from '../views/AdminUser/create.vue'
import AdminUserList from '../views/AdminUser/List.vue'
import Class from '../views/Class/create.vue'
import ClassList from '../views/Class/List.vue'
import Student from '../views/Student/create.vue'
import StudentList from '../views/Student/List.vue'
import SportItem from '../views/SportItem/create.vue'
import SportItemList from '../views/SportItem/List.vue'
import Apply from '../views/Apply/create.vue'
import ApplyList from '../views/Apply/List.vue'
import sportManege from '../views/sportManege/index.vue'
import sportSearch from '../views/sportSearch/index.vue'



const router = new VueRouter({
    routes: [
      { path: '/', redirect: '/login' },
      { path: '/login',component:Login },
      {
        path: '/home',
        component: Home,
        children: [
          { path: '/sportSearch', component: sportSearch },
          { path: '/sportManage', component: sportManege },
          { path: '/apply/create', component: Apply },
          { path: '/apply/create/:id', component: Apply ,props:true},
          { path: '/apply/list', component: ApplyList },
          { path: '/sportItem/create', component: SportItem },
          { path: '/sportItem/create/:id', component: SportItem ,props:true},
          { path: '/sportItem/list', component: SportItemList },
            { path: '/categroes/create', component: Categroes },
            { path: '/categroes/create/:id', component: Categroes ,props:true},
            { path: '/categroes/list', component: CategroesList },
            { path: '/items/create', component: Items },
            { path: '/items/create/:id', component: Items ,props:true},
            { path: '/items/list', component: ItemsList },
            { path: '/heroes/create', component: Heroes },
            { path: '/heroes/create/:id', component: Heroes ,props:true},
            { path: '/heroes/list', component: HeroesList },
            { path: '/admin_users/create', component: AdminUser },
            { path: '/admin_users/create/:id', component: AdminUser ,props:true},
            { path: '/admin_users/list', component: AdminUserList },
            { path: '/class/create', component: Class },
            { path: '/class/create/:id', component: Class ,props:true},
            { path: '/class/list', component: ClassList },
            { path: '/Student/create', component: Student },
            { path: '/Student/create/:id', component: Student ,props:true},
            { path: '/Student/list', component: StudentList },
            { path: '/Student/list', component: StudentList },
        ]
      }
    ]
  })


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

    export default router